<?php
/**
 * Created by PhpStorm.
 * User: tonyliangli
 * Date: 2016/10/20
 * Time: 16:11
 */

namespace Api\Controller;

use Think\Controller;

class IndexController extends CommonController {

    public function panelList() {

        $list = D("solarPanel")->select();

        $this->returnData['values']['list'] = $list;

        $this->response($this->returnData,'json');

    }

    public function checkMyWork() {

        $token = I("post.token");

        $service = D("service")->where(array("token"=>$token))->find();

        $warning = D("warning")->where(array(
            "service_id"=>$service["service_id"],
            "status"=>1
        ))->find();

        $hasWork = !isNullOrEmpty($warning);

        $this->returnData['values']['has_work'] = $hasWork;
        $this->returnData["values"]["warning_id"] = $warning["warning_id"];

        $this->response($this->returnData,'json');

    }

    /**
     * 获取结点数据
     */
    public function receiveData() {

        $id = I("post.id");

        $voltage = I("post.voltage");
        $current = I("post.current");
        $temperature = I("post.temperature");
        $humidity = I("post.humidity");

        $logData["panel_id"] = $id;
        $logData["voltage"] = $voltage;
        $logData["current"] = $current;
        $logData["temperature"] = $temperature;
        $logData["humidity"] = $humidity;
        $logData["create_time"] = getCurrentTime();
        $logData["day"] = getCurrentDate();

        D("solarPanelLog")->add($logData);

        $warningMap["panel_id"] = $id;
        $warningMap["status"] = -1;

        $warning = D("warning")->where($warningMap)->find();

        $panel = D("solarPanel")->where(array("panel_id"=>$id))->find();
        $area = D("area")->where(array("area_id"=>$panel["area_id"]))->find();
        $region = D("region")->where(array("region_id"=>$area["region_id"]))->find();

        if(isNullOrEmpty($warning)) {

            if($voltage > $region["voltage_upper_limit"] || $voltage < $region["voltage_lower_limit"]) {
                $warning["panel_id"] = $panel["panel_id"];
                $warning["warning_type"] = 1;
                $warning["status"] = -1;
                $warning["note"] = "电压异常，建议携带工具";
                $warning["create_time"] = getCurrentTime();
                $warning["tool"] = "万用表1个、钳子1个、冷却剂3瓶、手电筒1个、扳手1个、太阳能发电板2块、绝缘线若干";
                D("warning")->add($warning);
                D("solarPanel")
                    ->where(array("panel_id"=>$id))
                    ->save(array("status"=>-1, "note"=>$warning["note"]));
            } else if($current > $region["current_upper_limit"] || $current < $region["current_lower_limit"]) {
                $warning["panel_id"] = $panel["panel_id"];
                $warning["warning_type"] = 2;
                $warning["status"] = -1;
                $warning["note"] = "电流异常，建议携带工具";
                $warning["create_time"] = getCurrentTime();
                $warning["tool"] = "万用表1个、钳子1个、冷却剂3瓶、手电筒1个、扳手1个、太阳能发电板2块、绝缘线若干";
                D("warning")->add($warning);
                D("solarPanel")
                    ->where(array("panel_id"=>$id))
                    ->save(array("status"=>-1, "note"=>$warning["note"]));
            } else if($temperature > $region["temperature_upper_limit"] || $temperature < $region["temperature_lower_limit"]) {
                $warning["panel_id"] = $panel["panel_id"];
                $warning["warning_type"] = 3;
                $warning["status"] = -1;
                $warning["note"] = "温度异常，建议携带工具";
                $warning["create_time"] = getCurrentTime();
                $warning["tool"] = "万用表1个、钳子1个、冷却剂3瓶、手电筒1个、扳手1个、太阳能发电板2块、绝缘线若干";
                D("warning")->add($warning);
                D("solarPanel")
                    ->where(array("panel_id"=>$id))
                    ->save(array("status"=>-1, "note"=>$warning["note"]));
            } else if($humidity > $region["humidity_upper_limit"] || $humidity < $region["humidity_lower_limit"]) {
                $warning["panel_id"] = $panel["panel_id"];
                $warning["warning_type"] = 4;
                $warning["status"] = -1;
                $warning["note"] = "湿度异常，建议携带工具";
                $warning["create_time"] = getCurrentTime();
                $warning["tool"] = "万用表1个、钳子1个、冷却剂3瓶、手电筒1个、扳手1个、太阳能发电板2块、绝缘线若干";
                D("warning")->add($warning);
                D("solarPanel")
                    ->where(array("panel_id"=>$id))
                    ->save(array("status"=>-1, "note"=>$warning["note"]));
            }

        }
        $this->response($this->returnData,'json');

    }

    /**
     * 待维修列表
     */
    public function waitingList() {

        $token = I("post.token");

        $map["s.token"] = $token;
        $map["w.status"] = 1;

        $list = D("warning")->alias("w")
            ->join("left join __SOLAR_PANEL__ p on p.panel_id = w.panel_id")
            ->join("left join __SERVICE__ s on s.service_id = w.service_id")
            ->join("left join __AREA__ a on a.area_id = p.area_id")
            ->where($map)
            ->field("w.*,p.number,a.name as area_name,p.latitude,p.longitude")
            ->order("w.create_time desc")
            ->select();

        $this->returnData['values']['list'] = $list;

        $this->response($this->returnData,'json');

    }

    /**
     * 已维修列表
     */
    public function endList() {

        $token = I("post.token");

        $map["s.token"] = $token;
        $map["w.status"] = 2;

        $list = D("warning")->alias("w")
            ->join("left join __SOLAR_PANEL__ p on p.panel_id = w.panel_id")
            ->join("left join __SERVICE__ s on s.service_id = w.service_id")
            ->join("left join __AREA__ a on a.area_id = p.area_id")
            ->where($map)
            ->field("w.*,p.number,a.name as area_name,p.latitude,p.longitude")
            ->order("w.create_time desc")
            ->select();

        $this->returnData['values']['list'] = $list;

        $this->response($this->returnData,'json');

    }

    /**
     * 确认完成维修
     */
    public function endRepair() {

        $token = I("post.token");
        $id = I("post.id");

        $map["warning_id"] = $id;

        $warning = D("warning")->where($map)->find();

        $data["status"] = 2;
        $data["end_time"] = getCurrentTime();

        D("warning")->where($map)->save($data);

        D("service")->where(array("service_id"=>$warning["service_id"]))
            ->save(array("status"=>1));
        D("solarPanel")->where(array("panel_id"=>$warning["panel_id"]))
            ->save(array("status"=>1));

        $this->response($this->returnData,'json');

    }

    /**
     * 结点最近10个点的电流电压温湿度
     */
    public function lastData() {

        $id = I("post.id");

        $list = D("solarPanelLog")
            ->where(array("panel_id"=>$id))
            ->order("create_time desc")
            ->limit("10")->select();

        $voltage = array();
        $current = array();
        $temperature = array();
        $humidity = array();
        $time = array();

        $j = 0;

        for($i=count($list)-1; $i>=0; $i--,$j++) {
            $voltage[$j] = $list[$i]["voltage"];
            $current[$j] = $list[$i]["current"];
            $temperature[$j] = $list[$i]["temperature"];
            $humidity[$j] = $list[$i]["humidity"];
            $time[$j] = substr($list[$i]["create_time"], 11);
        }

        $this->returnData["voltage"] = $voltage;
        $this->returnData["current"] = $current;
        $this->returnData["temperature"] = $temperature;
        $this->returnData["humidity"] = $humidity;
        $this->returnData["time"] = $time;

        $this->response($this->returnData,'json');

    }

}